package com.loveto.service;

import com.loveto.common.ImageDownloader;
import com.loveto.constant.LovetoConstant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

public class UserinfoViewPhotoActivity extends Activity implements
View.OnClickListener{
	
	//변수 선언
	private String memberid;
	private int photonum;
	private ImageView v_photo;
	private ProgressDialog progressDialog;
	private Handler myHandler = new Handler();	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loveto_userinfo_view_photo);
		
		//변수 설정
		Intent intent = getIntent();
		memberid = intent.getStringExtra("memberid");
		photonum = intent.getIntExtra("photonum", 1);
		v_photo = (ImageView) findViewById(R.id.photo);
		progressDialog = new ProgressDialog(this);
		
		//클릭 리스너 달기
		RelativeLayout v_btn_goto_back = (RelativeLayout)findViewById(R.id.btn_goto_back);
		v_btn_goto_back.setOnClickListener(this);
		
		//시작 시 서버에서 사진 받아오기 
		getPhotoFromServer();
		
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_goto_back:
			finish();
			break;
		}
	}
	
    private void getPhotoFromServer() {
		new Thread(new Runnable(){
			@Override
			public void run() {
				//UI 를 건들기 때문에 핸들러 사용.
				myHandler.post(new Runnable() {
					@Override
					public void run() {
						progressDialog.setMessage("사진을 불러오고있습니다.");
						progressDialog.show();
							String photoUrl;
							ImageDownloader imageDownloader = new ImageDownloader();
				    		photoUrl = LovetoConstant.PHOTO_URL + memberid + "_" + photonum + ".jpg";
				    		imageDownloader.download(photoUrl, v_photo);
				    	progressDialog.dismiss();
					}
				});
			}
		}).start();
    }

}
